<template>
  <div class="p20 bg-white console h100 relative">
    <div class="mt10 mb20">
      <el-button
        class="theme-btn"
        @click="handleAdd"
        v-if="permitIds.includes('manag_staff_add')"
        >员工录入</el-button
      >
      <span class="ml20">
        <i class="label">所属部门：</i>
        <el-select
          placeholder="所属部门"
          v-model="dept"
          @change="handleGetJob"
          clearable
        >
          <el-option
            :key="item.id"
            :value="item.id"
            :label="item.name"
            v-for="item in deptList"
          ></el-option>
        </el-select>
      </span>
      <span class="ml20">
        <i class="label">所属岗位：</i>
        <el-select
          placeholder="所属岗位"
          v-model="job"
          @change="handleUpdate"
          clearable
        >
          <el-option
            v-for="item in jobList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </span>
      <Search :placeholder="placeholder" @update="handleUpdate"></Search>
      <span class="theme ml20">初始密码：123456</span>
    </div>
    <div class="import" v-if="permitIds.includes('manag_staff_import')">
      <el-button class="theme-btn">导入</el-button>
    </div>
    <div class="export" v-if="permitIds.includes('manag_staff_export')">
      <el-button @click="handlExportExcel">导出</el-button>
    </div>
    <el-table
      border
      stripe
      :data="tableData"
      v-loading="loading"
      @selection-change="handlSelect"
      element-loading-text="拼命加载中"
    >
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column prop="workCode" label="工号"></el-table-column>
      <el-table-column prop="realName" label="员工姓名"></el-table-column>
      <el-table-column prop="phone" label="手机号码"></el-table-column>
      <el-table-column prop="idCard" label="身份证号码"></el-table-column>
      <el-table-column prop="photo" label="照片">
        <template slot-scope="scope" v-if="scope.row.photo">
          <img class="thumbnail" :src="scope.row.photo" alt />
        </template>
      </el-table-column>
      <el-table-column prop="deptName" label="所属部门"></el-table-column>
      <el-table-column prop="jobName" label="所属岗位"></el-table-column>
      <el-table-column prop="operation" label="操作" width="280">
        <template slot-scope="scope">
          <el-button
            size="mini"
            class="theme-btn"
            @click="handleDetail(scope.row.id)"
            v-if="permitIds.includes('manag_staff_load')"
            >查看详情</el-button
          >
          <el-button
            size="mini"
            @click="handleEdit(scope.row.id)"
            v-if="permitIds.includes('manag_staff_update')"
            >修改</el-button
          >
          <el-button
            size="mini"
            @click="handleDelete(scope.row.id)"
            v-if="permitIds.includes('manag_staff_delete')"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :pageTotal="pageTotal"
      @paginationChange="handleUpdate"
    ></Pagination>
  </div>
</template>

<script>
import {
  getAllDept,
  getJobByDept,
  getAllInnerUser,
  deleteInnerUser
} from "@/apis/user";
import { mapGetters } from "vuex";
import Search from "@/components/search";
import Pagination from "@/components/pagination";

export default {
  name: "Inner_user_manage",
  data() {
    return {
      job: "",
      dept: "",
      jobList: [],
      deptList: [],
      userForm: {},
      pageTotal: 0,
      tableData: [],
      exportExcelData: [],
      placeholder: "输入员工姓名搜索",
      tHeader: [
        "工号",
        "员工姓名",
        "手机号码",
        "身份证号码",
        "所属部门",
        "所属岗位"
      ],
      filterVal: [
        "workCode",
        "realName",
        "phone",
        "idCard",
        "deptName",
        "jobName"
      ]
    };
  },
  created() {
    this.$store.dispatch("load/setLoading", true);
    this.$store.dispatch("pagination/resetPageNum");
    this.getUsers(this.pageSize, this.pageNum);
    this.getDepts(1);
  },
  computed: {
    ...mapGetters(["pageSize", "pageNum", "permitIds", "loading"])
  },
  methods: {
    async getUsers(pageSize, pageNum) {
      const { object } = await getAllInnerUser({
        pageNum,
        pageSize,
        jobId: this.job,
        queryStr: this.queryStr,
        departmentId: this.dept
      });

      this.pageTotal = object.total;
      this.tableData = object.records;
      this.$store.dispatch("load/setLoading", false);
    },
    async getDepts(propertyId) {
      const { rows } = await getAllDept({ propertyId });

      this.deptList = rows;
    },
    async handleGetJob() {
      this.job = "";
      this.jobList = [];
      this.getUsers(this.pageSize, this.pageNum);
      const { rows } = await getJobByDept({
        departmentId: this.dept
      });

      this.jobList = rows;
    },
    hanleClose() {
      this.editDialog = false;
      this.$store.dispatch("load/setLoading", false);
    },
    handleUpdate() {
      this.getUsers(this.pageSize, this.pageNum);
    },
    handleAdd() {
      this.$router.push({ name: "addUser" });
    },
    handleDetail(id) {
      this.$router.push({
        name: "detailUser",
        query: {
          id
        }
      });
    },
    handlExportExcel() {
      if (!this.exportExcelData.length) {
        this.$confirm("是否导出所有数据？", {
          distinguishCancelAndClose: true,
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        })
          .then(_ => {
            const params = {
              pageNum: 1,
              jobId: this.job,
              queryStr: this.queryStr,
              departmentId: this.dept,
              pageSize: this.pageTotal
            };

            // getAllInnerUser(params).then(res => {
            //   this.$store.dispatch("exportExcel/setExportExcel", {
            //     tHeader: this.tHeader,
            //     filterVal: this.filterVal,
            //     exportExcelData: res.object.records
            //   });
            // });
            this.$store.dispatch("exportExcel/setExportExcel", {
              tHeader: this.tHeader,
              filterVal: this.filterVal,
              exportExcelData: this.exportExcelData
            });
          })
          .catch(action => {
            this.$message({
              type: "info",
              message: "取消导出所有操作"
            });
          });
      } else {
        this.$store.dispatch("exportExcel/setExportExcel", {
          tHeader: this.tHeader,
          filterVal: this.filterVal,
          exportExcelData: this.exportExcelData
        });
      }
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该条, 是否继续?", "提示", {
        type: "warning",
        cancelButtonText: "取消",
        confirmButtonText: "确定"
      })
        .then(() => {
          this.$store.dispatch("load/setLoading", true);
          // deleteInnerUser({ internalUserId: id }).then(res => {
          //   if (res && res.code === "200") {
          //     this.$notify({
          //       title: "删除成功！",
          //       type: "success"
          //     });
          //     this.handleUpdate();
          //     this.$store.dispatch("load/setLoading", false);
          //   }
          // });
          this.$notify({
            title: "操作成功！",
            type: "success"
          });
          this.$store.dispatch("load/setLoading", false);
          this.handleUpdate();
        })
        .catch(() => {
          this.$message("已取消删除");
        });
    },
    handlSelect(selection) {
      this.exportExcelData = selection;
    },
    handleEdit(id) {
      this.$router.push({
        name: "editUser",
        query: {
          id
        }
      });
    }
  },
  components: {
    Search,
    Pagination
  }
};
</script>

<style scoped lang="less"></style>
